/**
 * 版权所有 2009-2012山东新北洋信息技术股份有限公司
 * 保留所有权利。
 */
package com.linyaonan.leetcode.easy._119;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: leetcode
 * @Package: com.linyaonan.leetcode.easy._119
 * @ClassName: PascalsTriangleTwo
 * @Author: linyaonan
 * @Date: 2019/11/5 15:29
 */
public class PascalsTriangleTwo {

    public List<Integer> getRow(int rowIndex) {
        List<Integer> resultList = new ArrayList<>();
        if (rowIndex <= -1) {
            return resultList;
        }
        resultList.add(1);
        if (rowIndex == 0) {
            return resultList;
        }
        resultList.add(1);
        if (rowIndex == 1) {
            return resultList;
        }
        int count = 2;
        while (count <= rowIndex) {
            resultList = getNextListFromThis(resultList);
            count++;
        }
        return resultList;
    }

    /**
     * 使用通项公式解法
     * @param rowIndex
     * @return
     */
    private List<Integer> getRowNew(int rowIndex) {
        List<Integer> res = new ArrayList<>(rowIndex + 1);
        long cur = 1;
        for (int i = 0; i <= rowIndex; i++) {
            res.add((int) cur);
            cur = cur * (rowIndex-i)/(i+1);
        }
        return res;
    }

    private List<Integer> getNextListFromThis(List<Integer> list) {
        List<Integer> iList = new ArrayList<>();
        if (list!= null && list.size() != 0) {
            // 头数据
            iList.add(1);
            for (int i = 0; i < list.size() - 1; i+=1) {
                Integer lNum = list.get(i);
                Integer rNum = list.get(i + 1);
                iList.add(lNum+rNum);
            }
            iList.add(1);
        }
        return iList;
    }

}
